<template>
  <div>
    <van-form ref="form">
      <van-field
        name="uploader"
        label="退款金额(元)"
        required
        :rules="rules.refundAmount"
      >
        <template #input>
          <van-stepper
            v-model="updRefundFormData.refundAmount"
            class="stepBox"
            :decimal-length="2"
            min="0.01"
            :max="Math.min(updRefundFormData.amount, 99999999.99)"
          />
        </template>
      </van-field>
      <van-field
        v-model="updRefundFormData.remark"
        rows="5"
        name="备注"
        label="备注"
        type="textarea"
        maxlength="256"
        placeholder="请输入备注"
        show-word-limit
        :rules="rules.remark"
      />
      <van-field name="uploader" label="附件">
        <template #input>
          <uploadCom
            ref="uploadRef"
            @uploadFileIds="uploadFileIds"
            :defaultFiles="defaultFiles"
            accept=".jpg,.jpeg,.png"
          ></uploadCom>
        </template>
      </van-field>
    </van-form>
    <full-loading v-if="loading"></full-loading>
  </div>
</template>

<script>
import { queryFileList } from "@/api/common";
import {
  refundDetall,
  refundOrder,
} from "@/api/srvAssurance/shoppingMall/order";

export default {
  name: "refund",
  data() {
    return {
      loading: false,
      updRefundFormData: {
        fileCodes: [],
        orderNo: "",
        amount: 0,
        refundAmount: null,
        refundId: "",
        remark: "",
      },
      rules: {
        refundAmount: [
          {
            required: true,
            type: "number",
            message: "请输入退款金额",
            trigger: "onBlur",
          },
          {
            pattern: /(^(\d{0,8})$)|(^(\d{0,8}\.\d{0,2})$)/,
            message: "整数不超过8位，小数不超过2位",
            trigger: "onBlur",
          },
        ],
        remark: [
          { message: "请输入备注", trigger: "onBlur" },
          {
            pattern: this.$commonReg.textAreaReg,
            message: this.$commonReg.textAreaMsg,
            trigger: "onBlur",
          },
        ],
      },
      defaultFiles: [],
    };
  },
  methods: {
    // 退款
    getOrderData(item) {
      this.$refs.form.resetValidation();
      this.$refs.uploadRef.reset();
      this.updRefundFormData = {
        refundId: "",
        orderNo: item.orderNo,
        amount: item.orderAmount,
        refundAmount: item.orderAmount,
        remark: "",
        fileCodes: [],
      };
      this.defaultFiles = [];
      if (item.afterSalesStatus === "2") {
        this.refundDeail(item.orderNo);
      }
    },
    // 查询退款信息
    refundDeail(orderNo) {
      this.loading = true;
      refundDetall(orderNo)
        .then((res) => {
          this.loading = false;
          if (res.code === 1000 && res.data) {
            this.updRefundFormData.refundId = res.data.refundId;
            this.updRefundFormData.refundAmount = res.data.refundAmount;
            this.updRefundFormData.remark = res.data.remark;
            this.updRefundFormData.fileCodes = res.data.fileCodes || [];
            if (res.data.fileCodes && res.data.fileCodes.length) {
              this.queryRefundFileInfoData(res.data.fileCodes);
            }
          }
        })
        .catch(() => {
          this.loading = false;
        });
    },
    // 获取退款图片信息
    queryRefundFileInfoData(fileCodeArr) {
      queryFileList(fileCodeArr).then((res) => {
        if (res.code === 1000 && res.data && res.data.length) {
          this.defaultFiles = res.data;
        }
      });
    },
    // 附件标识
    uploadFileIds(arr) {
      this.updRefundFormData.fileCodes = arr;
    },
    // 确定退款
    handleUpdRefundSure() {
      this.$refs.form.validate().then(() => {
        let params = {
          orderNo: this.updRefundFormData.orderNo,
          refundAmount: Number(this.updRefundFormData.refundAmount),
          remark: this.updRefundFormData.remark,
          fileCodes: this.updRefundFormData.fileCodes,
        };
        if (this.updRefundFormData.refundId) {
          params.refundId = this.updRefundFormData.refundId;
        }
        this.loading = true;
        refundOrder(params)
          .then((res) => {
            this.loading = false;
            if (res.code === 1000) {
              this.$Notify({ type: "success", message: "申请退款成功！" });
              this.$emit("close");
              this.$emit("refresh");
            }
          })
          .catch(() => {
            this.loading = false;
          });
      });
    },
  },
};
</script>

<style scoped lang="less">
.stepBox /deep/.van-stepper__input {
  width: 120px;
}
</style>
